package com.baoleme.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baoleme.dto.LoginDTO;

import com.baoleme.entity.Member;
import com.baoleme.exceptions.BusinessException;
import com.baoleme.exceptions.enumeration.ResponseEnum;
import com.baoleme.mapper.MemberMapper;
import com.baoleme.service.MemberService;
import com.baoleme.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-19
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Resource
    JwtUtils jwtUtils;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public String login(LoginDTO loginDTO) {
        //2.构造条件
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getName, loginDTO.getName());


        //1. 根据用户名或者密码查询用户信息
        Member memberDb = this.getOne(wrapper);

        //判断是否得到用户
        if (memberDb == null) {
            throw new BusinessException(ResponseEnum.INVALID_ACCOUNT);
        }
        //判断用户是否被禁用
        if (memberDb.getStatus() == 0) {
            throw new BusinessException(ResponseEnum.ERROR_DISABLED_ACCOUNT);
        }

        //比较密码
        if (!BCrypt.checkpw(loginDTO.getPassword(), memberDb.getPassword())) {
            throw new BusinessException(ResponseEnum.INVALID_ACCOUNT);
        }

        //签发token
        String token = jwtUtils.createJwt(
                memberDb.getId(),
                null,
                60 * 60 * 24 * 7L,
                "123456789123456789123456789123456789123456789123456789123456789123456789123456789"
        );

        redisTemplate.opsForValue().set(memberDb.getName(),token,1800, TimeUnit.SECONDS);

        return token;



    }



    @Override
    public boolean saveUser(Member member) {
        //编写查询条件
        LambdaQueryWrapper<Member> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Member::getName, member.getName())
                .or()
                .eq(Member::getPhone, member.getPhone())
                .or()
                .eq(Member::getIdCard, member.getIdCard());

        //将查询条件放进查询中
        long count = this.count(wrapper);

        //比对查询条件如果存在重复将直接跳出
        if(count>0){
            throw new BusinessException(ResponseEnum.DUPLICATE_DATA);
        }

        //将密码加密后存入数据库
        member.setPassword(BCrypt.hashpw(member.getPassword()));
        //将新员工信息存入数据库
        boolean save = this.save(member);

        return save;
    }

    @Override
    public boolean logout(String name) {

        //2.构造条件
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getName, name);


        //1. 根据用户名或者密码查询用户信息
        Member memberDb = this.getOne(wrapper);

        //判断是否得到用户
        if (memberDb == null) {
            throw new BusinessException(ResponseEnum.INVALID_ACCOUNT);
        }

        redisTemplate.delete(memberDb.getName());

        return true;
    }
}
